//
//  LYDTabBarView.m
//  20150809生活圈
//
//  Created by yons on 15/8/11.
//  Copyright (c) 2015年 iron. All rights reserved.
//

#import "LYDTabBarView.h"
#import "LYDTabBarViewAndPopViewDelegate.h"
#define tabBarHeight 49
@interface LYDTabBarView ()

@property (nonatomic, strong) UIView *tabBarView;

@end

@implementation LYDTabBarView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        NSInteger tabBarItemCount = 5;
        for (NSInteger index = 0; index < tabBarItemCount; index++) {
            UIButton *btn = [LYDFactory LYDButtonwithType:LYDNormalButton WithFrame:CGRectMake(index * (UIScreenWidth / tabBarItemCount), 0, UIScreenWidth / 5, tabBarHeight) withTitle:nil withTag:index];
            NSString *normalImage = [NSString stringWithFormat:@"home_%zd",index];
            [btn setBackgroundImage:[UIImage imageNamed:normalImage] forState:UIControlStateNormal];
            NSString *selectedImage = [NSString stringWithFormat:@"home_%zd_pressed",index];
            [btn setBackgroundImage:[UIImage imageNamed:selectedImage] forState:UIControlStateSelected];
            [btn addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btn];
            if (index == 0) {
                btn.selected = YES;
            }
        }
    }
    return self;
}

- (void)tabBarButtonClick:(UIButton *)btn
{
    [self.subviews enumerateObjectsUsingBlock:^(UIButton *tabBarItem, NSUInteger idx, BOOL *stop) {
        tabBarItem.selected = NO;
    }];

    btn.selected = YES;
    if ([self.delegate respondsToSelector:@selector(changeControllerWithButton:)]) {
        [self.delegate changeControllerWithButton:btn];
    }
}

@end
